package edu.cmu.cs.cs214.hw9.client.gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;

import edu.cmu.cs.cs214.hw9.client.controllers.Event;
import edu.cmu.cs.cs214.hw9.client.controllers.FacelookEvent;
import edu.cmu.cs.cs214.hw9.client.controllers.StatusSelector;
import edu.cmu.cs.cs214.hw9.datatypes.Status;

/**
 * Shows a logged-in user's newsfeed. 
 */
public class NewsFeedScreen extends FacelookScreen {
	private static final long serialVersionUID = -5186164799622518788L;
	private JList statusPanelList;
	
	/**
	 * Actionlistener which searches the database for a particular user. 
	 */
	private class SearchFriends implements ActionListener {
		private JTextField searchField;
		
		public SearchFriends(JTextField searchField) {
			this.searchField = searchField;
		}
		
		public void actionPerformed(ActionEvent e) {
			fireEventOccured(new FacelookEvent(Event.SEARCH_FRIENDS, searchField.getText()));
		}
	}

	/**
	 * Constructs a news feed screen based on the news items provided in status list.
	 * 
	 * @param statusList The list of statuses to display in the news feed.
	 */
	public NewsFeedScreen(List<Status> statusList) {
		// Layout init.
		GridBagConstraints c = new GridBagConstraints();
		
		JLabel statusLabel = new JLabel("Friends' statuses");
		statusLabel.setForeground(Color.white);
		
		// Buttons for navigation and search.
		JButton searchButton = new JButton("Search People");
		JButton friendsButton = new JButton("My Friends");
		JButton profileButton = new JButton("My Profile");
		
		JTextField searchField = new JTextField();
		searchField.setColumns(20);
		
		// Setup news feed. We use a custom renderer for the news feed.
		statusPanelList = new JList(statusList.toArray(new Status[statusList.size()]));
		statusPanelList.setCellRenderer(new StatusRenderer());
		statusPanelList.setBackground(new Color(59, 89, 152));
		
		JScrollPane statusScroller = new JScrollPane(statusPanelList);
		
		// Make the search button search for users.
		searchButton.addActionListener(new SearchFriends(searchField));
		
		// Profile button goes to the current user's profile page.
		profileButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fireEventOccured(new FacelookEvent(Event.GO_TO_PROFILE, null));
			}
		});
		
		// Friends button goes to the list of the current user's friends.
		friendsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fireEventOccured(new FacelookEvent(Event.GO_TO_FRIENDS, null));
			}
		});
		
		// Make clicking on any item in the news feed go to the profile of the
		// user who posted the status.
		statusPanelList.addListSelectionListener(new StatusSelector(this));

		
		// Add the components to the layout.
		c.gridx = 0;
		add(profileButton, c);
		
		c.gridx = 1;
		add(friendsButton, c);
		
		c.gridx = 2;
		add(searchField, c);
		
		c.gridx = 3;
		add(searchButton, c);
		
		c.gridx = 0;
		add(statusLabel, c);
		
		c.gridx = 0;
		c.gridwidth = 4;
		c.fill = GridBagConstraints.HORIZONTAL;
		add(statusScroller, c);
	}
}
